<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="appnode.Node" %>
<%@ page import="appnode.SearchNode" %>
<%@ page import="appnode.RateUtils" %>
<%@ page import="appnode.Link" %>
<%@ page import="appnode.PMF" %>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory;"%>


<head>
	<link rel="stylesheet" href="map.css" type="text/css" media="screen" title="mapcss"></link>
	<script>
	function checkform() {
		valid=true;
		if (document.getElementById("name").innerHTML==""){
			alert("Fill name");
			valid=false;	
		}
	return valid;
	}
	</script>
</head>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;
key=ABQIAAAA93mmG_9GVHrZQc0uDY9JhxR53orP37CVGrhoHFm0nIrb80EZexTyT3lbUmmiWSijKVtWh7Cq2qkFJQ"
 type="text/javascript"></script>

<script>

var marker; 

   function initialize() {
     if (GBrowserIsCompatible()) {
       var map = new GMap2(document.getElementById("map"));
       map.setCenter(new GLatLng(37.982634,23.698196), 12);
	   map.setMapType(G_PHYSICAL_MAP); 

       GEvent.addListener(map, "click", function(overlay, latlng) {
         if (latlng) {
           marker = new GMarker(latlng);
           GEvent.addListener(marker, "click", function() {
             var html = "<div><table style='padding:5px'>" +
						"<form action='addnode' method='post'><tr><td>Latitude:</td><td>"+ latlng.lat() + "<input type='hidden' name='lat' value="+latlng.lat()+"></input></td> </tr>" +
						"<tr><td>Longtitude:</td> <td>"+ latlng.lng() + "<input type='hidden' name='lng' value="+latlng.lng()+"></td> </tr>" +                            
                        "<tr><td>Name:</td> <td><input type='text' name='nodename' id='name'/> </td> </tr>" +              
                        "<tr><td></td><td><input type='submit' value='Save' onsubmit='return checkform()'/></form></td></tr>";
						"</table></div>"
			 
           marker.openInfoWindow(html);
           });
		   map.clearOverlays(); 
           map.addOverlay(marker);
         }
       });
	 
     }
   }
	
   function saveData() {
     var name = escape(document.getElementById("name").value);
     var latlng = marker.getLatLng();
     var lat = latlng.lat();
     var lng = latlng.lng();
	 if (name=="") {
		alert("Fill name");	
	 }
	else {
		var xmlhttp;
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    document.getElementById("table").innerHTML=xmlhttp.responseText;
		    }
		  }
		xmlhttp.open("POST","/addnode",true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send("nodename=" + name + "&lat=" + lat + "&lng=" + lng);
		}
   
   }
</script>
<%
 UserService userService = UserServiceFactory.getUserService();
 User user = userService.getCurrentUser();
 String url = "/";
%>

<%if (user!=null) {
%>
<body onload="initialize()" onunload="GUnload()">
	<div>
   	<table cellspacing="0" cellpadding="0" class="maptabletd">
		<tr>
		     <td colspan="2" id="header" class="maptabletd"><div id="banner">AppMapNode</div>
					<div id="menu">		
								<a href="<%= userService.createLogoutURL(url)%>">Sign out</a>
					</div>		
			</td>
		  </tr>

	  <tr>
	    <td class="maptabletd">
	       <div id="map"></div>
	    </td>
		<td valign="top" id="leftside" class="maptabletd">
			<div id="side_bar0">
				<%
				String u = user.getNickname();
				PersistenceManager pm = PMF.get().getPersistenceManager();
			    String query = "select from " + Node.class.getName() + " where owner==" + "'"+ u +"'";
			    List<Node> results = (List<Node>) pm.newQuery(query).execute();
				if (results.isEmpty()) {
				%>
				You have not node
				<p>Click on the map to add a new node
				</div>
				
					<%
					} else {
						%>
						Your nodes :<br>
						<%
						for (Node n : results) {
				
					
					 out.print(n.getNodename());
					}
				    }
					%>			
			<div id="barchoice"><br>
				
			</div>
		</td>		
	  </tr>
	  <tr>
	  </tr>
	</table>
	</div>
	<div id="image"><img src="http://code.google.com/appengine/images/appengine-noborder-120x30.gif" 
	alt="Powered by Google App Engine" /></div>
   <div id="message"></div>
<%
}
else {
%>
You must <a href="<%= userService.createLoginURL(url) %>">(sign in)</a>
<%
}
%>
 </body>

</html>




